package com.yanqu.road.dao.impl.activity.recallplayer;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.activity.recallplayer.config.RecallPlayerDrawConfig;
import com.yanqu.road.entity.activity.recallplayer.config.RecallPlayerSingleSimpleRewardConfig;
import com.yanqu.road.entity.activity.recallplayer.config.RecallPlayerTypeConfig;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class RecallPlayerConfigDaoImpl extends TempDao {
    private static String getRecallPlayerDrawConfigSql = "select * from `t_s_activity_recallplayer_draw` where `activityId`=? ORDER BY `floor` ASC";

    public Map<Integer, RecallPlayerDrawConfig> getRecallPlayerDrawConfigMap(int activityId) {
        Map<Integer, RecallPlayerDrawConfig> recallPlayerDrawConfigMap = new HashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getRecallPlayerDrawConfigSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                RecallPlayerDrawConfig recallPlayerDrawConfig = new RecallPlayerDrawConfig();
                recallPlayerDrawConfig.setActivityId(rs.getInt("activityId"));
                recallPlayerDrawConfig.setFloor(rs.getInt("floor"));
                recallPlayerDrawConfig.setConsume(rs.getInt("consume"));
                recallPlayerDrawConfig.setSpecialReward(StringUtils.stringToStringList(rs.getString("specialReward"), "\\|"));

                String simpleRewards = rs.getString("rewards");
                List<RecallPlayerSingleSimpleRewardConfig> simpleRewardConfigs = new ArrayList<>();
                List<String> rewardList = StringUtils.stringToStringList(simpleRewards, "\\|");
                for (String string : rewardList) {
                    RecallPlayerSingleSimpleRewardConfig simpleRewardConfig = new RecallPlayerSingleSimpleRewardConfig();

                    List<String> rewardAndTimeWeight = StringUtils.stringToStringList(string, ",");
                    simpleRewardConfig.setReward(rewardAndTimeWeight.get(0));
                    simpleRewardConfig.setOneTimeWeight(StringUtils.string2intSafe(rewardAndTimeWeight.get(1),1));
                    simpleRewardConfigs.add(simpleRewardConfig);
                }
                recallPlayerDrawConfig.setSimpleRewardConfigs(simpleRewardConfigs);
                recallPlayerDrawConfig.setSpecialRewardWeights(rs.getInt("specialRewardWeights"));
                recallPlayerDrawConfig.setRewardsWeights(rs.getInt("rewardsWeights"));
                recallPlayerDrawConfig.setShowValue(rs.getInt("showValue"));

                recallPlayerDrawConfigMap.put(recallPlayerDrawConfig.getFloor(), recallPlayerDrawConfig);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return recallPlayerDrawConfigMap;
    }

    private static String getRecallPlayerTypeConfigSql = "select * from `t_s_activity_recallplayer_money` where `activityId`=? ORDER BY `id` ASC";

    public List<RecallPlayerTypeConfig> getRecallPlayerTypeConfigList(int activityId) {
        List<RecallPlayerTypeConfig> list = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getRecallPlayerTypeConfigSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                RecallPlayerTypeConfig recallPlayerTypeConfig = new RecallPlayerTypeConfig();
                recallPlayerTypeConfig.setActivityId(rs.getInt("activityId"));
                recallPlayerTypeConfig.setId(rs.getInt("id"));
                recallPlayerTypeConfig.setIcon(rs.getString("icon"));
                recallPlayerTypeConfig.setName(rs.getString("name"));
                recallPlayerTypeConfig.setScore(rs.getLong("score"));
                recallPlayerTypeConfig.setDrawNum(rs.getInt("drawNum"));
                list.add(recallPlayerTypeConfig);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }
}
